from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from appinput.models import App
from envx.models import Env
from public.models import CommonInfo

class DeployStatus(CommonInfo):
    memo = models.CharField(max_length=1024, blank=True,
                            verbose_name='备注')

IS_INC_TOT_CHOICES = (
    ('TOT', r'全量部署'),
    ('INC', r'增量部署'),
)

DEPLOY_TUYPE_CHOICE =(
    ('deployall', r'发布所有'),
    ('deploypkg', r'发布程序'),
    ('deploypcfg', r'发布配置'),
    ('rollback', r'回滚'),
)

class DeployPool(CommonInfo):
    name = models.CharField(max_length=100, blank=True, null=True,
                            verbose_name='发布单号')
    description = models.CharField(max_length=1024, blank=True,
                            verbose_name='描述')
    app_name = models.ForeignKey(App, related_name='deploy_app',
                                 on_delete=models.CASCADE, verbose_name='APP 应用')
    # 本发布单创建以来，供发布多少次
    deploy_no = models.IntegerField(blank=True, null=True,default=0)
    # 所对应的git版本
    branch_build= models.CharField(max_length=255, null=True, blank=True)
    # Jenkins 编译的次数
    jenkins_number = models.CharField(max_length=255, null=True, blank=True)
    # Git 内部区别版本号
    code_number = models.CharField(max_length=255, null=True, blank=True)
    is_inc_tot = models.CharField(max_length=255, choices=IS_INC_TOT_CHOICES,
                                  blank=True, null=True, verbose_name='全量或增量部署')
    deploy_type = models.CharField(max_length=255, choices=DEPLOY_TUYPE_CHOICE,
                                  blank=True, null=True, verbose_name='发布程序或配置')
    is_build = models.BooleanField(default=False, verbose_name='软件是否编译成功')
    create_user = models.ForeignKey(User, related_name='deploy_create_user',
                                    on_delete=models.CASCADE, verbose_name='创建用户')
    nginx_url = models.URLField(default=None, blank=True,
                                null=True,verbose_name='Tengine URL')
    env_name = models.ForeignKey(Env, blank=True, null=True,
                                 related_name='deploy_env_name', on_delete=models.CASCADE,
                                 verbose_name='环境')
    deploy_status = models.ForeignKey(DeployStatus, related_name='depoly_pool_status',
                                      blank=True, null=True,
                                      on_delete=models.CASCADE, verbose_name='发布单状态')

class Histtory(CommonInfo):
    user = models.ForeignKey(User, blank=True, null=True,
                             related_name='history_user', on_delete=models.CASCADE,
                             verbose_name='用户')
    app_name = models.ForeignKey(App, blank=True, null=True,
                             related_name= 'history_app', on_delete=models.CASCADE,
                             verbose_name='APP 应用')
    env_name = models.ForeignKey(Env, blank=True, null=True,
                             related_name= 'history_env_name', on_delete=models.CASCADE,
                             verbose_name='环境')
    deploy_name = models.ForeignKey(DeployPool, blank=True, null=True,
                             related_name= 'history_deploy', on_delete=models.CASCADE,
                             verbose_name='发布单')
    do_type = models.CharField(max_length=32, blank=True, null=True,
                               verbose_name='操作类型')
    content = models.CharField(max_length=1024, blank=True, null=True, verbose_name='操作内容')